package com.brianxd.mvc.controller;

import com.brianxd.mvc.bean.TestBean;
import com.brianxd.mvc.bean.User;
import com.brianxd.mvc.service.intf.IUserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

@Controller
@RequestMapping("/")
public class HelloController implements ApplicationContextAware {

	private ApplicationContext context;

	@Resource
	private IUserService userService;

	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

		User user = userService.getUserById(1);
		TestBean bean = (TestBean)context.getBean("testBean");
		model.addAttribute("message", bean.getTest() + user.getUserName());
		return "hello";
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		this.context = applicationContext;
	}
}